/**
 * 提问
 */
var currQuesids = ''; //已有笔记
var quesList = new Array();
var plusStorageManagers = require('script/plusStorageManagers.js');
//新增问题
mui('header').on('tap', '.openputques', openPutques);
//保存提问
mui('.putques-div').on('tap', '.submitques', savePutques);
//取消提问
mui('.putques-div').on('tap','.cancelques',cancelQues);
//点击回答按钮事件
mui('.questions').on('tap','.question .answerbtn',openAnswerques);
//点击回答按钮事件,,,
mui('.questions').on('tap','.question .childanswerbtn',openAnswerques2);
//同问
mui('.questions').on('tap','.sameask',sameAsk);
//加载问题信息
function loadQuestions() {
	document.getElementById('quescataloginfo').innerHTML='';
	document.getElementById('questions').innerHTML = '<div class="noquestip">没有选择资源或该资源还没有提问</div>';
	currQuesids = ''; //已有资源
	quesList = new Array();
	if(hasNet()){
		loadQuestionAjax();
	}else{
		//loadQuestionLocal();
	}
};
//初始化questiondiv
function initQuestionsDiv(){
		var catalogname=document.getElementById('fCatalogname').value;
	    var resname=document.getElementById('fResname').value;
	    var rescatname=document.getElementById('fRescatname').value;
	    var preclass=getCatClass(rescatname);
        if (resname != null && resname.length > 15) resname = resname.substring(0, 15) + '...';
	    var str='<div class="ques-catalogname"><div class="catalog-name"><p>'+catalogname+'</p></div></div>'
										+'<div class="ques-restype"><div class="c1"><span class="restype-div '+preclass+'">'+rescatname+'</span><span class="resname-div">'+resname+'</span></div></div>';
	    document.getElementById('quescataloginfo').innerHTML=str;
}
//加载本地提问
function loadQuestionLocal(){
	var catalogid=document.getElementById('fCatalogid').value;
	var resid=document.getElementById('fResid').value;
	//显示本地缓存资源
	var quesList = plusStorageManagers.getQuessSessionByResid(catalogid, resid);
	if (quesList != null && quesList.length > 0){
		clearQuessDiv(quesList);
		var tempList=quesList;
     	buildQuesDivs(tempList, false);
     }
}
//加载ajax提问
function loadQuestionAjax(){
	var catalogid=document.getElementById('fCatalogid').value;
	var resid=document.getElementById('fResid').value;
	mui.ajax(serverAjax + 'mobile/question/getQuestionByResidAndStudentid.do', {
		data: {
			catalogid: catalogid,
			resid: resid,
			currQuesids: currQuesids
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		success: function(data) {
			if (data != null && data.length > 0) clearQuessDiv(quesList);
			if (data == null || data.length == 0) return;
			buildQuesDivs(data, true);
			//mui.toast("data" + data.length);
           //alert(quesList);
			plusStorageManagers.setQuessSessionByResid(catalogid, resid, quesList);
		},
		error: function(xhr, type, errorThrown) {
			console.log(xhr);
			ajaxErrorTip();
		}
	});
}
//构建1
function buildQuesDivs(quesListTemp, ifAjax) {
	if (quesListTemp == null || quesListTemp == '') return;
	if(quesListTemp.length>0)initQuestionsDiv();
	for (var i = 0; i < quesListTemp.length; i++) {
		var question = quesListTemp[i];
		if (!ifAjax) currQuesids += question.qstId + ",";
		if (ifAjax) quesList.push(question); //ajax加载的内容需要添加到缓存
		buildQuesDiv(question);
	}
	if (currQuesids.length > 0) currQuesids = currQuesids.substring(0, currQuesids.length - 1);
};
//构建2
function buildQuesDiv(question) {
	var parentid = question.parentId;
	var content=question.content;
	if(content.indexOf('~')!=-1){
			content='<span style="color:#7b7b7b;margin-right:10px;">'+content.substring(0,content.indexOf('~'))+":</span>"+content.substring(content.indexOf('~')+2,content.length);
	}
	if (parentid == 0) {
		var questionsdiv = document.getElementById('questions');
		var classname=question.className;
		if(classname.length>6)classname=classname.substring(0,4)+'...';
		var questionstr = '<div class="question">' + '<div class="ques-content">' + '<div class="ques-title"><span class="ck">&nbsp;</span><span class="cc">' + content + '</span><span class="yd">老师已答</span></div>' + '<div class="ques-elseinfo"><span>' + classname + '</span><span class="quesuser">' + question.creator + '</span><span class="sameask" questionid="' + question.qstId + '">同问(</span><span>' + question.askNum + '</span>)<span class="answerbtn" questionid="'+question.qstId+'">回复</span></div>' + '</div>' + '<div class="ques-answers" id="parentquestionid' + question.qstId + '"></div>' + '</div>';
		questionsdiv.innerHTML = questionsdiv.innerHTML + questionstr;
	} else {
		var parentquestioniddiv = document.getElementById('parentquestionid' + question.parentId);
		//第一层提问
		if (parentquestioniddiv != null) {
			var teachertag = '';
			if (question.answerType == 0) teachertag = '(老师)';
			var reply=question.creator + teachertag;
			
			var questionstr = '<div class="answer" id="answerid"' + question.qstId + '>' + '<div class="answeruser"><span>' + question.creator + teachertag + '</span><span class="date">'+question.createTime+'</span></div>' + '<div class="answer-content"><span class="hfc">' + content + '</span><span class="childanswerbtn" questionid="'+question.qstId+'" parentid="'+question.parentId+'" reply="'+reply+'">回复</span></div>' + '</div>';
			parentquestioniddiv.innerHTML = parentquestioniddiv.innerHTML + questionstr;
		} else {
			//var parentansweriddiv=document.getElementById('answerid'+question.parentId);
			//if(parentansweriddiv!=null){
			//  var parentansweridsEle=parentansweriddiv.getParent;	
			//  var newdivEle=document.createElement('div');
			//  newdivEle.className='answer';
			//  newdivEle.id='answerid'+question.qstId;
			//  parentansweridsEle.insertAfter(newdivEle,parentansweridsEle);
			//}
		}
	}
};
//清空原本内容
function clearQuessDiv() {
	//清空原本内容
	document.getElementById('questions').innerHTML = '';
};
//打开新增问题框
var replyto='';
function openPutques() {
	pauseVideo();
	replyto='';
	//var currWebview=plus.webview.currentWebview();
	//currWebview.setStyle({
	//			mask: 'rgba(0,0,0,0.4)',
	//			transition: {
	//				duration: 10
	//			}
	//		});
	var resid = document.getElementById('fResid').value;
	if (resid == 0 || resid == '') {
		mui.toast('请选择资源');
		return;
	}
	document.getElementById('fParentid').value=0;
	document.getElementById('questextarea').value = '';
	document.getElementById('putques-div').style.display = 'block';
};
//保存问题
function savePutques() {
	if(!hasNet())return;
	var userid = localStorageManagers.getUseridSession();
	var courseid = document.getElementById('fCourseid').value;
	var catalogid = document.getElementById('fCatalogid').value;
	var resid = document.getElementById('fResid').value;
	var quesContent = document.getElementById('questextarea').value;
	var parentId=document.getElementById('fParentid').value;
	if (quesContent.length == 0) {
		mui.toast("请填写问题内容");
		return;
	}
	if (quesContent.length > 200) {
		mui.toast("最多不超过200字！");
		return;
	}
	quesContent=replyto+quesContent;
	var data={
			userId: userid,
			resId: resid,
			catalogId: catalogid,
			courseId: courseid,
			questionContent: quesContent,
			parentId:parentId
	};
	ajaxSubmitQuestion(data);
	document.getElementById('putques-div').style.display = 'none';
	document.getElementById('questextarea').value = '';
	document.getElementById('questextarea').placeholder="请输入您的回复...";
	playVideo();
}
//ajax保存问题
function ajaxSubmitQuestion(data){
	mui.ajax(serverAjax + '/mobile/question/SaveQuestion.do', {
		type: 'post',
		dataType: 'json',
		data: data,
		success: function(result) {
             if(result!=0){
             	mui.toast('保存成功');
             	loadQuestions();
             }else{
             	mui.toast('保存失败');
             }
		},
		error: function(xhr, type, errorThrown) {
			console.log(xhr);
		}
	});
};
//打开回复问题框
function openAnswerques() {
	pauseVideo();
	replyto='';
	var resid = document.getElementById('fResid').value;
	if (resid == 0 || resid == '') {
		mui.toast('请选择资源');
		return;
	}
	var questionid=this.getAttribute('questionid');
	document.getElementById('fParentid').value=questionid;
	document.getElementById('questextarea').value = '';
	document.getElementById('putques-div').style.display = 'block';
};
function openAnswerques2() {
	pauseVideo();
	var resid = document.getElementById('fResid').value;
	if (resid == 0 || resid == '') {
		mui.toast('请选择资源');
		return;
	}
	var reply=this.getAttribute('reply');
	replyto="回复 "+reply+"~     ";
	var parentid=this.getAttribute('parentid');
	//var reply=this.getAttribute('reply');
	document.getElementById('fParentid').value=parentid;
	//document.getElementById('questextarea').value = "回复 "+reply+"： ";
	document.getElementById('questextarea').placeholder="回复 "+reply+":";
	document.getElementById('putques-div').style.display = 'block';
};
//取消问题回复框
function cancelQues(){
	document.getElementById('putques-div').style.display = 'none';
	playVideo();
}
//同问
function sameAsk(){
	if(!hasNet())return;
	var thisBtn=this;
	var userid = localStorageManagers.getUseridSession();
	var courseid = document.getElementById('fCourseid').value;
	var catalogid = document.getElementById('fCatalogid').value;
	var resid = document.getElementById('fResid').value;
	var questionid=thisBtn.getAttribute('questionid');
	var data={
		studentId:userid,
		courseId:courseid,
		catalogId:catalogid,
		resId:resid,
		questionId:questionid
	};
	mui.ajax(serverAjax + '/mobile/question/SaveQuestionAsk.do', {
		type: 'post',
		dataType: 'json',
		data: data,
		success: function(result) {
             if(result==1){
             	mui.toast('同问');
             	thisBtn.nextSibling.innerHTML=parseInt(thisBtn.nextSibling.innerHTML,10)+1;
             }else if(result==2){
             	thisBtn.nextSibling.innerHTML=parseInt(thisBtn.nextSibling.innerHTML,10)-1;
             	mui.toast('取消同问');
             }if(result==0){
             	mui.toast('不能同问自己提的问题');
             }
		},
		error: function(xhr, type, errorThrown) {
			console.log(xhr);
		}
	});
	
}
